<template>
  <UploadExcel :on-success="onSuccess" />
</template>

<script>
import { importEmployees } from '@/api/employees'
export default {
  methods: {
    async onSuccess(data) {
      console.log(data.results)
      const users = data.results.map((user) => {
        return this.zh2en(user)
      })
      console.log(users)
      await importEmployees(users)
      this.$message.success('上传成功')
      this.$router.back()
    },
    // 转化数据 键转为英文
    zh2en(user) {
      /* user = {
      入职日期: 43535
      姓名: "陈斯朗"
      工号: 20089
      手机号: 13041139879
      转正日期: 43719
      } */
      const newUsers = {}
      const dict = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber'
      }
      for (const key in user) {
        //   timeOfEntry
        const newKey = dict[key]
        //   43535
        const newVal = user[key]
        if (newKey === 'timeOfEntry' || newKey === 'correctionTime') {
          newUsers[newKey] = this.formatDate(newVal)
        } else {
          // timeOfEntry:43535
          newUsers[newKey] = newVal
        }
      }
      return newUsers
    },
    formatDate(numb, format = '-') {
      const time = new Date((numb - 1) * 24 * 3600000 + 1)
      time.setYear(time.getFullYear() - 70)
      const year = time.getFullYear() + ''
      const month = time.getMonth() + 1 + ''
      const date = time.getDate() + ''
      if (format && format.length === 1) {
        return year + format + month + format + date
      }
      return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date)
    }

  }
}
</script>

<style>
</style>
